using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;

public class FadeManager : MonoBehaviour
{
    public static FadeManager Instance { get; private set; }
    private CanvasGroup canvasGroup;
    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        canvasGroup = GetComponent<CanvasGroup>();
        if (canvasGroup == null)
        {
            canvasGroup = gameObject.AddComponent<CanvasGroup>(); // 如果没有 CanvasGroup 组件，则添加一个
        }

        canvasGroup.alpha = 0;
        gameObject.SetActive(false); // 初始时设置不可见
    }

    // 异步的渐入效果
    public async Task FadeIn(float duration = 0.2f)
    {
        gameObject.SetActive(true);
        await canvasGroup.DOFade(1, duration).AsyncWaitForCompletion(); // 使用 DOTween 的 AsyncWaitForCompletion 来等待渐变完成
    }

    // 异步的渐出效果
    public async Task FadeOut(float duration = 0.2f)
    {
        await canvasGroup.DOFade(0, duration).AsyncWaitForCompletion(); // 渐出完成后禁用 GameObject
        gameObject.SetActive(false);
    }

    public async Task WaitSeconds(float duration = 0.2f)
    {
        // 等待指定的时间（秒转换为毫秒）
        await Task.Delay((int)(duration * 1000));
    }

}
